package tw.stock;

import java.util.ArrayList;  
import android.app.ActivityGroup;  
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.MenuInflater;  
import android.view.MenuItem;  
import android.view.View;
import android.widget.Button;
  
  
public class FirstGroup extends ActivityGroup {  
  
        // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view  
    public static FirstGroup group;  
  
        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.  
    private ArrayList<View> history;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          
          
          //Pause widget
          
          
          
          //this.history = new ArrayList<view>();  
          history = new ArrayList<View>();
          group = this;  
  
          
              // Start the root activity withing the group and get its view  
          
          try
          {
	           View view = getLocalActivityManager().startActivity("CitiesActivity", new  
	                                            Intent(this,stockwidget.class)  
	                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
	                                            .getDecorView();  
	  
	           stockwidget stockList = (stockwidget)view.getContext();
	          // stockList.EnterFavorite();
	           replaceView(view);
	              // Replace the view of this ActivityGroup  
          }
          catch(Exception e)
	  	    {
	  	    	String str = e.toString();
	  	    	str = "";
	  	    	
	  	    }
            
  
       }  
  
    public void replaceView(View v) {  
                // Adds the old one to history  
        history.add(v);  
                // Changes this Groups View to the new View.  
        setContentView(v);  
    }  
  
    public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);  
            setContentView(history.get(history.size()-1));  
        }else {  
        	Intent Myintent=new Intent(this, Widget.class);
    	    Myintent.setAction(Widget.START);
    		this.sendBroadcast(Myintent);
        	
        	//finish();
        }  
    }  
  
   public void onBackPressed() {  
        FirstGroup.group.back();  
        return;  
    }  
   
   public void onResume()
   {
	   super.onResume();
	   View view = getLocalActivityManager().startActivity("CitiesActivity", new  
               Intent(this,stockwidget.class)  
               .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
               .getDecorView();  

	   stockwidget stockList = (stockwidget)view.getContext();
	   stockList.EnterFavorite();
	   replaceView(view);
   }
  
} 